import 'package:flutter/material.dart';

class XinDongTabIndicator extends Decoration {
  final Color color;
  final double width;
  final double height;
  final double radius;

  const XinDongTabIndicator({this.color = Colors.black, this.width = 10.0, this.height = 4.0, this.radius = 2.0});

  @override
  BoxPainter createBoxPainter([VoidCallback? onChanged]) {
    return _XinDongTabIndicatorBoxPainter(color, width, height, radius);
  }
}

class _XinDongTabIndicatorBoxPainter extends BoxPainter {
  final Color color;
  final double indicatorWidth;
  final double indicatorHeight;
  final double radius;

  _XinDongTabIndicatorBoxPainter(this.color, this.indicatorWidth, this.indicatorHeight, this.radius);

  @override
  void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
    // 计算indicator的left、top、right、bottom
    final double height = configuration.size!.height;
    final Rect rect = Rect.fromLTWH(
      offset.dx + (configuration.size!.width - indicatorWidth) / 2,
      offset.dy + height - indicatorHeight - 4,
      indicatorWidth,
      indicatorHeight,
    );

    // 绘制indicator
    final Paint paint = Paint();
    paint.color = color;
    paint.style = PaintingStyle.fill;
    final RRect rrect = RRect.fromRectAndRadius(rect, Radius.circular(radius));
    canvas.drawRRect(rrect, paint);
  }
}
